package cn.zjnu.DAO;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Dao {
	Connection con=null;

	   protected Statement statement=null;

	   ResultSet rs=null;

	    String url="jdbc:mysql://localhost:3306/equip_manager";

	    String username="root"; 

	    String password="mysql";

	public void connect() throws ClassNotFoundException, SQLException{

	Class.forName("com.mysql.jdbc.Driver");

	con =DriverManager.getConnection(url,username,password);

	statement=con.createStatement();

	    }

	
	public boolean login(String UserId,String Pwd,String Type) throws SQLException{
		String mysql="select UserId,Pwd,RoleName from User,Role where UserId='"+UserId+"'";
		rs=statement.executeQuery(mysql);
		if(!rs.next()) return false;
		else if(rs.getString(2).equals(Pwd)&&rs.getString(3).equals(Type))
		return true;
		return false;
	}
	public boolean  register(String user,String password) throws SQLException{
		if(user!=null&&password!=null) {
			String mysqlString="insert into username values ('"+user+"','"+password+"')";
			statement.executeUpdate(mysqlString);
			return true;
		}
		else return false;
	}
	public ResultSet executeQuery(String no) throws SQLException{
		String sql1="select * from book where no="+no;
	rs=statement.executeQuery(sql1);

	return rs;                     

	 }

	public void close() throws SQLException{

	rs.close();

	statement.close();

	con.close(); 
	    }
}
